﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"                                
                xmlns:x="http://schemas.microsoft.com/xps/2005/06/resourcedictionary-key">

  <xsl:include href="../../XsltUtils.xslt"/>

  <!-- Template para página HTML -->
  <xsl:decimal-format name="decimalFormat" grouping-separator="," digit="#" zero-digit="0" decimal-separator="." />


  <xsl:variable name="ServerURL" select="/ProjectDataSet/General/ServerURL"/>


  <xsl:variable name="currentProposalUId" select="/ProjectDataSet/Project/CurrentProposalUId" />

  <xsl:variable name="ProposalUnits" select="//ProjectDataSet/Project/BacklogItemUnit[Type != 6 and HoursAtProposal &gt; 0]" />

  <xsl:key name="BacklogItem-by-GroupName" match="VisibleBacklogItem[IsAtProposal='true']" use="GroupName" />

  

  <!-- StyleSttings ********************************************  -->
  <!-- Defines the style attributes for the document.             -->
  <!-- ***********************************************************-->
  <xsl:template name="StyleSettings">
    <FlowDocument.Resources>
      
      <Style TargetType="Paragraph">
        <Setter Property="FontFamily" Value="Arial"/>
        <Setter Property="FontSize" Value="10"/>        
      </Style>

      <Style x:Key="SubTitleParagraph" TargetType="Paragraph">
        <Setter Property="FontFamily" Value="Arial"/>
        <Setter Property="FontSize" Value="12"/>
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="Margin" Value="0,10,0,5"/>
      </Style>
      
      <Style TargetType="Table">      
        <Setter Property="CellSpacing" Value="0"/>
        <Setter Property="BorderBrush" Value="Black"/>
        <Setter Property="BorderThickness" Value="0.5,0.5,0.5,0.5"/>
        <Setter Property="Margin" Value="0,0,0,5"/>
      </Style>

      <Style TargetType="TableCell">
        <Setter Property="Padding" Value="5,3,5,3"/>
        <Setter Property="BorderThickness" Value="0.5,0.5,0.5,0.5"/>
        <Setter Property="BorderBrush" Value="Black"/>
      </Style>

      <Style x:Key="HeaderCell" TargetType="TableCell">
        <Setter Property="Background" Value="Black"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Padding" Value="5,3,5,3"/>
        <Setter Property="BorderThickness" Value="0.5,0.5,0.5,0.5"/>
        <Setter Property="BorderBrush" Value="Black"/>
      </Style>

      <Style x:Key="GroupCell" TargetType="TableCell">
        <Setter Property="Background" Value="LightGray"/>
        <Setter Property="Foreground" Value="Black"/>
        <Setter Property="Padding" Value="5,3,5,3"/>
        <Setter Property="BorderThickness" Value="0.5,0.5,0.5,0.5"/>
        <Setter Property="BorderBrush" Value="Black"/>
      </Style>
            
      <Style x:Key="NoBorderTable" TargetType="Table">        
        <Setter Property="CellSpacing" Value="0"/>
        <Setter Property="BorderBrush" Value="White"/>
        <Setter Property="BorderThickness" Value="0,0,0,0"/>
      </Style>

      <Style x:Key="NoBorderCell" TargetType="TableCell">
        <Setter Property="BorderThickness" Value="0,0,0,0"/>        
      </Style>

    </FlowDocument.Resources>
  </xsl:template>  
  
  <!-- Header **************************************************  -->
  <!-- Shows the header text.                                     -->
  <!-- ***********************************************************-->
  <xsl:template name="Header">
    <Table Style="{{StaticResource NoBorderTable}}" Margin="0,0,0,0">
      <Table.Columns>
        <TableColumn />
        <TableColumn Width="182"/>
      </Table.Columns>
      <TableRowGroup>
        <TableRow>
          <TableCell Style="{{StaticResource NoBorderCell}}">
            <Table>
              <Table.Columns>
                <TableColumn Width="100"/>
                <TableColumn />
              </Table.Columns>
              <TableRowGroup>
                <TableRow>
                  <TableCell>
                    <Paragraph>Project</Paragraph>
                  </TableCell>
                  <TableCell>
                    <Paragraph>
                      <xsl:value-of select="/ProjectDataSet/Project/ProjectName"/>
                      [<xsl:value-of select="/ProjectDataSet/Project/ProjectNumber"/>]
                    </Paragraph>
                  </TableCell>
                </TableRow>
                <TableRow>
                  <TableCell>
                    <Paragraph>Customer</Paragraph>
                  </TableCell>
                  <TableCell>
                    <Paragraph>
                      <xsl:value-of select="/ProjectDataSet/Project/ClientName"/>
                    </Paragraph>
                  </TableCell>
                </TableRow>
                <TableRow>
                  <TableCell>
                    <Paragraph>Date</Paragraph>
                  </TableCell>
                  <TableCell>
                    <Paragraph>
                      <xsl:choose>
                        <xsl:when test="/ProjectDataSet/Proposal[ProposalUId=/ProjectDataSet/Project/CurrentProposalUId]/ApprovalDateString">
                          <xsl:value-of select="/ProjectDataSet/Proposal[ProposalUId=/ProjectDataSet/Project/CurrentProposalUId]/ApprovalDateString"/>
                        </xsl:when>
                        <xsl:otherwise>
                          <xsl:value-of select="/ProjectDataSet/Project/ProposalDate"/>
                        </xsl:otherwise>
                      </xsl:choose>
                    </Paragraph>
                  </TableCell>
                </TableRow>
                <TableRow>
                  <TableCell>
                    <Paragraph>Proposal</Paragraph>
                  </TableCell>
                  <TableCell>
                    <Paragraph>
                      <xsl:value-of select="/ProjectDataSet/Proposal[ProposalUId=/ProjectDataSet/Project/CurrentProposalUId]/ProposalName"/>
                    </Paragraph>
                  </TableCell>
                </TableRow>
              </TableRowGroup>
            </Table>
          </TableCell>
          <TableCell Style="{{StaticResource NoBorderCell}}">
            <TableCell.Background>
              <ImageBrush ImageSource="{$ServerURL}/FactoryService/Images/Company/logo.jpg" Stretch="None" />
            </TableCell.Background>
          </TableCell>
        </TableRow>
      </TableRowGroup>
    </Table>
              
  </xsl:template>

  <!-- Project Info *********************************************  -->
  <!-- Shows the project Info.                                     -->
  <!-- ************************************************************-->
  <xsl:template name="ProjectInfo">
    <xsl:param name="idx" select="1"/>    
    <Paragraph Style="{{StaticResource SubTitleParagraph}}">      
        <xsl:value-of select="number($idx)"/>. Technology
    </Paragraph>


    <Table>
      <TableRowGroup>
        <TableRow>
          <TableCell>
            <Paragraph>Main development technology:</Paragraph>
          </TableCell>
          <TableCell>
            <Paragraph>
              <xsl:value-of select="/ProjectDataSet/Project/Plataform"/>
            </Paragraph>
          </TableCell>
        </TableRow>
      </TableRowGroup>
    </Table>

    <Paragraph Style="{{StaticResource SubTitleParagraph}}">      
        <xsl:value-of select="number($idx)+1"/>. Project Description
    </Paragraph>
    <Table>
      <TableRowGroup>
        <TableRow>        
          <TableCell>
            <Paragraph>
              <xsl:if test="/ProjectDataSet/Project/Description">
                <xsl:call-template name="MultiLineText">
                  <xsl:with-param name="text" select="/ProjectDataSet/Project/Description"/>
                </xsl:call-template>                                   
              </xsl:if>
              <xsl:if test="not(/ProjectDataSet/Project/Description)">
                Not defined.
              </xsl:if>
            </Paragraph>
          </TableCell>
        </TableRow>
      </TableRowGroup>
    </Table>    

  </xsl:template>

  <!-- BackLogTable *********************************************  -->
  <!-- Shows the backlog table.                                    -->
  <!-- ************************************************************-->
  <xsl:template name="BackLogTable">
    <xsl:param name="idx" select="3"/>

    <Paragraph Style="{{StaticResource SubTitleParagraph}}">
      <xsl:value-of select="number($idx)"/>. Scope Items
    </Paragraph>
    <Table>
      <Table.Columns>
        <TableColumn />
        <xsl:for-each select="$ProposalUnits">
          <TableColumn Width="70"/>
        </xsl:for-each>
      </Table.Columns>
      <TableRowGroup>
        <TableRow>
          <TableCell Style="{{StaticResource HeaderCell}}">
            <Paragraph>Item</Paragraph>
          </TableCell>
          <xsl:for-each select="$ProposalUnits">
            <xsl:sort select="UnitName" />
            <TableCell Style="{{StaticResource HeaderCell}}" TextAlignment="Right">
              <Paragraph>
                <xsl:value-of select="UnitName"/> (hrs)
              </Paragraph>
            </TableCell>
          </xsl:for-each>
        </TableRow>

        <xsl:for-each select="/ProjectDataSet/VisibleBacklogItem[count(. | key('BacklogItem-by-GroupName', GroupName)[1]) = 1 and IsAtProposal='true']">
          <xsl:sort select="BusinessValue" data-type="number"  order="descending" />
          <xsl:sort select="BacklogItemNumber" data-type="number"  />

          <xsl:variable name="groupName" select="GroupName"/>

          <TableRow>
            <TableCell ColumnSpan="{count($ProposalUnits)+1}" Style="{{StaticResource GroupCell}}">
              <Paragraph>
                <xsl:value-of select="$groupName" />
              </Paragraph>
            </TableCell>
          </TableRow>
        
          <xsl:for-each select="key('BacklogItem-by-GroupName', GroupName)">
            <xsl:sort select="BusinessValue" data-type="number"  order="descending" />
            <xsl:sort select="BacklogItemNumber" data-type="number"  />                      
            <xsl:variable name="backlogItemUId" select="BacklogItemUId"/>
            <xsl:variable name="sprintUId" select="SprintUId"/>
            <xsl:variable name="baseLine" select="BaseLine"/>
            <TableRow>
              <TableCell>
                <Paragraph>
                  <xsl:value-of select="Title" />
                  <xsl:if test="Status='3'">
                    [CANCELED]
                  </xsl:if>
                  <xsl:if test="Description != ''">                    
                    <LineBreak/>
                    <Italic>                      
                      <xsl:call-template name="MultiLineText">
                        <xsl:with-param name="text" select="/ProjectDataSet/Project/BacklogItem[BacklogItemUId = $backlogItemUId]/Description"/>
                      </xsl:call-template>
                    </Italic>
                  </xsl:if>                  
                </Paragraph>
              </TableCell>
              <xsl:for-each select="$ProposalUnits">
                <xsl:sort select="UnitName" />
                <xsl:variable name="unitName" select="UnitName" />
                <TableCell TextAlignment="Right">
                  <Paragraph>
                    <xsl:choose>
                      <xsl:when test="//ProjectDataSet/Project/BacklogItem/BacklogItemHours[UnitName=$unitName and BacklogItemUId=$backlogItemUId and BaseLine=$baseLine and SprintUId=$sprintUId]/Quantity!=''">
                        <xsl:value-of select="//ProjectDataSet/Project/BacklogItem/BacklogItemHours[UnitName=$unitName and BacklogItemUId=$backlogItemUId and BaseLine=$baseLine and SprintUId=$sprintUId]/Quantity"/>
                      </xsl:when>
                      <xsl:otherwise>
                        0
                      </xsl:otherwise>
                    </xsl:choose>  
                  </Paragraph>
                </TableCell>
              </xsl:for-each>
            </TableRow>                          
        </xsl:for-each>            
      </xsl:for-each>
        <TableRow>
          <TableCell>
            <Paragraph>
              <Bold>Total Hours</Bold>
            </Paragraph>
          </TableCell>
          <xsl:for-each select="$ProposalUnits">
            <xsl:sort select="UnitName" />
            <xsl:variable name="unitName" select="UnitName" />
            <TableCell TextAlignment="Right">
              <Paragraph>
                <Bold>
                  <xsl:value-of select="HoursAtProposal"/>
                </Bold>
              </Paragraph>
            </TableCell>
          </xsl:for-each>
        </TableRow>
      </TableRowGroup>
    </Table>          
  </xsl:template>

  <xsl:template name="ProjectPrice">
    <xsl:param name="idx" select="4"/>
    <Paragraph Style="{{StaticResource SubTitleParagraph}}">
        <xsl:value-of select="number($idx)"/>. Price
    </Paragraph>

    <Table>
      <TableRowGroup>
        <xsl:if test="/ProjectDataSet/Proposal[ProposalUId=$currentProposalUId]/UseDiscount = 'true'">
          <TableRow>
            <TableCell>
              <Paragraph>Implementation</Paragraph>
            </TableCell>
            <TableCell TextAlignment="Right">
              <Paragraph>
                <xsl:call-template name="CurrencyFormat">
                  <xsl:with-param name="value" select="/ProjectDataSet/Proposal[ProposalUId=$currentProposalUId]/CalculatedBacklogPrice"/>
                </xsl:call-template>
              </Paragraph>
            </TableCell>
          </TableRow>
          <xsl:call-template name="ProposalFixedCostItems"/>
          <xsl:if test="/ProjectDataSet/Proposal[ProposalUId=$currentProposalUId]/Discount > 0">
            <TableRow>
              <TableCell>
                <Paragraph>- Discont</Paragraph>
              </TableCell>
              <TableCell TextAlignment="Right">
                <Paragraph>
                  <xsl:value-of select="format-number(/ProjectDataSet/Proposal[ProposalUId=$currentProposalUId]/Discount,'#,##0.00','decimalFormat')"/> %
                </Paragraph>                
              </TableCell>
            </TableRow>
          </xsl:if>
        </xsl:if>
        <TableRow>
          <TableCell>
            <Paragraph>
              <Bold>Total Price</Bold>
            </Paragraph>
          </TableCell>
          <TableCell TextAlignment="Right">
            <Paragraph>
              <Bold>
                <xsl:call-template name="CurrencyFormat">
                  <xsl:with-param name="value" select="/ProjectDataSet/Proposal[ProposalUId=$currentProposalUId]/SalePrice"/>
                </xsl:call-template>
              </Bold>
            </Paragraph>
          </TableCell>
        </TableRow>
      </TableRowGroup>
    </Table>          
  </xsl:template>

  <xsl:template name="ProjectHourValues">
    <xsl:param name="idx" select="4"/>
    <Paragraph Style="{{StaticResource SubTitleParagraph}}">      
        <xsl:value-of select="number($idx)"/>.1 Hour Value      
    </Paragraph>

    <Table>
      <TableRowGroup>
        <xsl:for-each select="$ProposalUnits">
          <TableRow>
            <TableCell>
              <Paragraph>
                <xsl:value-of select="UnitDescription"/> (<xsl:value-of select="UnitName"/>)
              </Paragraph>
            </TableCell>
            <TableCell TextAlignment="Right">
              <Paragraph>
                <xsl:call-template name="CurrencyFormat">
                  <xsl:with-param name="value" select="UnitSaleValue"/>
                </xsl:call-template>
              </Paragraph>
            </TableCell>
          </TableRow>
        </xsl:for-each>
      </TableRowGroup>
    </Table>
    <Paragraph>
      Any scope change required by the customer must have its impact and cost evaluated by Foster according the hour value table above, and need to be aproved by the customer.      
    </Paragraph> 
    
  </xsl:template>

  <xsl:template name="ProjectDeadLine">
    <xsl:param name="idx" select="5"/>
    <Paragraph Style="{{StaticResource SubTitleParagraph}}">      
        <xsl:value-of select="number($idx)"/>. Deadlines      
    </Paragraph>

    <Table>
      <TableRowGroup>
       
        <TableRow>
          <TableCell>
            <Paragraph>
              Estimated Project's Start Date:
            </Paragraph>
          </TableCell>
          <TableCell TextAlignment="Right">
            <Paragraph>
              <Bold>
                <xsl:value-of select="/ProjectDataSet/Project/EstimatedStartDateString"/>
              </Bold>
            </Paragraph>
          </TableCell>
        </TableRow>
        <TableRow>
          <TableCell>
            <Paragraph>
              Estimated Project's Finish Date:
            </Paragraph>
          </TableCell>
          <TableCell TextAlignment="Right">
            <Paragraph>
              <Bold>
                <xsl:value-of select="/ProjectDataSet/Project/EstimatedEndDateString"/>
              </Bold>
            </Paragraph>
          </TableCell>
        </TableRow>
      </TableRowGroup>
    </Table>

  </xsl:template>

  <xsl:template name="ProjectContacts">
    <xsl:param name="idx" select="6"/>
    <Paragraph Style="{{StaticResource SubTitleParagraph}}">      
        <xsl:value-of select="number($idx)"/>. Stakeholders      
    </Paragraph>

    <Table>
      <TableRowGroup>
        <xsl:for-each select="//ProjectDataSet/Project/BacklogItemUnit">
          <xsl:sort select="Type"/>
          <xsl:sort select="UnitDescription"/>
          <xsl:variable name="unitName" select="UnitName"/>
          <xsl:if test="count(/ProjectDataSet/Project/ProjectRole[UnitName=$unitName]) > 0">
            <TableRow>
              <TableCell>
                <Paragraph>
                  <Bold>
                    <xsl:value-of select="UnitDescription"/>
                  </Bold>
                </Paragraph>
              </TableCell>
              <TableCell>
                <List Margin="0">
                  <xsl:for-each select="/ProjectDataSet/Project/ProjectRole[UnitName=$unitName]">
                    <ListItem>
                      <Paragraph TextAlignment="Left">
                        <xsl:value-of select="ContactName"/>
                        <xsl:if test="string-length(ContactEmail) > 0">
                          (<xsl:value-of select="ContactEmail"/>)
                        </xsl:if>
                      </Paragraph>
                    </ListItem>
                  </xsl:for-each>
                </List>
              </TableCell>
            </TableRow>
          </xsl:if>
        </xsl:for-each>
      </TableRowGroup>
    </Table>

  </xsl:template>

  <xsl:template name="ProposalFixedCostItems">  
      
      <xsl:for-each select="/ProjectDataSet/Proposal/ProposalFixedCostItem[ProposalUId=/ProjectDataSet/Project/CurrentProposalUId]">
        <TableRow>
          <TableCell>
            <Paragraph>
              + <xsl:value-of select="ItemName"/>
            </Paragraph>
          </TableCell>
          <TableCell TextAlignment="Right">
            <Paragraph>
              <xsl:call-template name="CurrencyFormat">
                <xsl:with-param name="value" select="ItemValue"/>
              </xsl:call-template>
            </Paragraph>
          </TableCell>
        </TableRow>
      </xsl:for-each>
          
  </xsl:template>


  <xsl:template name="ProposalObservations">
    <xsl:param name="idx" select="6"/>

    <xsl:if test="count(/ProjectDataSet/Proposal/ProposalObservation[ProposalUId=/ProjectDataSet/Project/CurrentProposalUId])>0">    
        <xsl:for-each select="/ProjectDataSet/Proposal/ProposalObservation[ProposalUId=/ProjectDataSet/Project/CurrentProposalUId]">
          <xsl:sort select="Order" data-type="number"/>
          <Paragraph Style="{{StaticResource SubTitleParagraph}}">            
              <xsl:value-of select="number($idx)+number(Order)"/>. <xsl:value-of select="ObservationName"/>            
          </Paragraph>          
          <Table>
            <TableRowGroup>
              <TableRow>
                <TableCell>
                  <Paragraph>
                    <xsl:value-of select="ObservationText"/>
                  </Paragraph>
                </TableCell>
              </TableRow>
            </TableRowGroup>
          </Table>          
        </xsl:for-each>
    </xsl:if>

  </xsl:template>

  <xsl:template name="ProposalPayments">
    <xsl:param name="idx" select="7"/>
    <xsl:if test="count(/ProjectDataSet/Proposal/ProjectPayment[ProposalUId=/ProjectDataSet/Project/CurrentProposalUId])>0 or /ProjectDataSet/Project/PaymentProjectUId">
      <Paragraph Style="{{StaticResource SubTitleParagraph}}">        
          <xsl:value-of select="number($idx)"/>. Project´s Planned Payments        
      </Paragraph>
    </xsl:if>
    <Table>
      <TableRowGroup>
        <xsl:choose>
          <xsl:when test="not(/ProjectDataSet/Project/PaymentProjectUId)">

            <xsl:if test="count(/ProjectDataSet/Proposal/ProjectPayment[ProposalUId=/ProjectDataSet/Project/CurrentProposalUId])>0">
              <xsl:for-each select="/ProjectDataSet/Proposal/ProjectPayment[ProposalUId=/ProjectDataSet/Project/CurrentProposalUId]">
                <xsl:sort select="PlannedPayDate" data-type="text"/>
                <TableRow>
                  <TableCell>
                    <Paragraph>
                      <xsl:value-of select="PlannedPayDateString"/>
                    </Paragraph>
                  </TableCell>
                  <TableCell TextAlignment="Right">
                    <Paragraph>
                      <xsl:call-template name="CurrencyFormat">
                        <xsl:with-param name="value" select="Value"/>
                      </xsl:call-template>
                    </Paragraph>
                  </TableCell>
                </TableRow>
              </xsl:for-each>
              <TableRow>
                <TableCell></TableCell>
                <TableCell>
                  <Paragraph>
                    <Bold>
                      <xsl:call-template name="CurrencyFormat">
                        <xsl:with-param name="value" select="sum(/ProjectDataSet/Proposal/ProjectPayment[ProposalUId=/ProjectDataSet/Project/CurrentProposalUId]/Value)"/>
                      </xsl:call-template>
                    </Bold>
                  </Paragraph>
                </TableCell>
              </TableRow>
            </xsl:if>            
          </xsl:when>
          <xsl:otherwise>
            <TableRow>
              <TableCell>
                <Paragraph>
                  This project will be debited from another project.
                </Paragraph>
              </TableCell>
            </TableRow>            
          </xsl:otherwise>
        </xsl:choose>
      </TableRowGroup>
    </Table>
      
  </xsl:template>

  <xsl:template name="ApprovedStamp">
    <xsl:if test="/ProjectDataSet/Proposal[ProposalUId=/ProjectDataSet/Project/CurrentProposalUId]/Status=4">              
          <!--BlockUIContainer >
            <Image Source="images/notapproved.gif" Stretch="None" Margin="0,-100,0,0"/>
          </BlockUIContainer-->            
    </xsl:if>

  </xsl:template>

  <xsl:template name="CurrencyFormat">
    <xsl:param name="value"/>
    <xsl:value-of select="/ProjectDataSet/Proposal[ProposalUId=$currentProposalUId]/MoneyShortName" />&#160;<xsl:value-of select="format-number(number($value) * /ProjectDataSet/Proposal[ProposalUId=$currentProposalUId]/MoneyRate ,'#,##0.00','decimalFormat')" />
  </xsl:template>
  
  
</xsl:stylesheet>

